import GameManager from "../common/GameManager";
import Utils from "../common/Util";

export default class PropManager extends cc.EventTarget {
    public static INIT_PROP_COUNT = 1;

    public static TYPE_FINGER = 1;
    public static TYPE_HAMMER = 2;

    private static _instance = null;
    private _porpsNum = [0, PropManager.INIT_PROP_COUNT, PropManager.INIT_PROP_COUNT];
    constructor() {
        super();
        this.reset();
    }

    public static getInstance(): PropManager {
        if (!PropManager._instance) {
            PropManager._instance = new PropManager();
        }
        return PropManager._instance;
    }

    /**
     * 改变道具数量
     * @param index 
     */
    public changeProp(index: number, add: number) {
        this.propNum[index] += add;
        GameManager.getInstance().gameData.SetReceivedProps(index, add);
        GameManager.getInstance().eventCenter.DispatchEvent("propChange", { index: index, add: add });
    }

    public reset() {
        this._porpsNum = [0, PropManager.INIT_PROP_COUNT, PropManager.INIT_PROP_COUNT];
    }

    public get propNum(): number[] {
        return this._porpsNum;
    }

    public set propNum(n) {
        this._porpsNum = n;
    }

    public ranAddProp(): number {
        function weightRandom() {
            var randomConfig = [{ id: 0, weight: 0 }, { id: 1, weight: 5 }, { id: 2, weight: 5 }];
            var randomList = [];
            for (var i in randomConfig) {
                for (var j = 0; j < randomConfig[i].weight; j++) {
                    randomList.push(randomConfig[i].id);
                }
            }
            var randomValue = randomList[Math.floor(Math.random() * randomList.length)];
            return randomValue;
        };

        let ran = weightRandom();
        return ran;
    }

    public getRandomProp(probabilities: number[]): number {
        var probability = Math.random();
        for (var i = 0; i < probabilities.length; i++) {
            var start = 0;
            var j = 0;
            for (; j < i; j++) {
                start += probabilities[j];
            }
            var end = start + probabilities[j];
            if (probability > start && probability <= end) {
                return i;
            }
        }
        return 0;
    }

    /**
     * 打开宝箱
     */
    public openTreasure(): void {
        cc.loader.loadRes("prefab/TrasureChest", cc.Prefab, (err, prefab) => {
            if (prefab) {
                var Anim_Node = cc.instantiate(prefab);
                Anim_Node.x = 0;
                Anim_Node.y = 0;
                Anim_Node.parent = cc.find("Canvas");
                var anim = Utils.GetComponent(Anim_Node, cc.Animation);
                if (anim) {
                    anim.play(name);
                }
            }
        });
    }

    public openPropLayer(type: number, count: number): void {
        cc.loader.loadRes("prefab/PropLayer.prefab", cc.Prefab, (err, prefab) => {
            if (prefab) {
                var Anim_Node = cc.instantiate(prefab);
                var PropLayer = Utils.GetComponent(Anim_Node, "PropLayer");
                PropLayer.init(type, count)
                Anim_Node.x = 0;
                Anim_Node.y = 0;
                Anim_Node.parent = cc.find("Canvas");
                var anim = Utils.GetComponent(Anim_Node, cc.Animation);
                if (anim) {
                    anim.play(name);
                }
            }
        });
    }

}